package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.StringCodeOperationActivity;
import com.example.sunmiv1.entity.RefuseDetailInfo;
import com.example.sunmiv1.utils.Tools;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RefuseDetailAdapter extends BaseAdapter {

    private List<RefuseDetailInfo.DataBean.ListBean> listBeans;
    private ViewHolder viewHolder;
    private Context context;

    public RefuseDetailAdapter(List<RefuseDetailInfo.DataBean.ListBean> listBeans) {
        this.listBeans = listBeans;
    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.refuse_detail_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        context = parent.getContext();
        RefuseDetailInfo.DataBean.ListBean listBean = listBeans.get(position);
        viewHolder.goodsName.setText(listBean.getGoods_name());
        Tools.hideViewInItem(listBean.getSku_name(), viewHolder.skuName);
        viewHolder.goodsCode.setText(listBean.getGoods_code());
        viewHolder.goodsNumber.setText(listBean.getGoods_number());
        Tools.hideViewInItem(listBean.getCompany(), viewHolder.goodsUnit);
        Tools.hideViewInItem(listBean.getSku_goods_number(), viewHolder.skuNumber);
        viewHolder.infoOutWarehousePrice.setText(listBean.getStock_price() + "");
        viewHolder.infoOutWarehouseNumber.setText(listBean.getRefuse_num() + "");
        viewHolder.infoMoneyTotal.setText(listBean.getSubtotal() + "");
        viewHolder.infoRemark.setText(listBean.getRemarks());
        if ("1".equals(listBean.getSerial_number_state())) {
            viewHolder.infoOutWarehouseNumber.setTextColor(ContextCompat.getColor(context, R.color.blue_3293FA));
        } else {
            viewHolder.infoOutWarehouseNumber.setTextColor(ContextCompat.getColor(context, R.color.text_666));
        }
        final int index = position;
        viewHolder.infoOutWarehouseNumber.setOnClickListener(v -> {
            if ("1".equals(listBeans.get(index).getSerial_number_state())) {
                Intent intent = new Intent(parent.getContext(), StringCodeOperationActivity.class);
                intent.putExtra("operation", 19);
                intent.putExtra("id", listBeans.get(index).getId());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.goods_name)
        TextView goodsName;
        @BindView(R.id.layout_goods_name)
        RelativeLayout layoutGoodsName;
        @BindView(R.id.sku_name)
        TextView skuName;
        @BindView(R.id.layout_sku_name)
        RelativeLayout layoutSkuName;
        @BindView(R.id.goods_code)
        TextView goodsCode;
        @BindView(R.id.layout_goods_code)
        RelativeLayout layoutGoodsCode;
        @BindView(R.id.goods_number)
        TextView goodsNumber;
        @BindView(R.id.layout_goods_number)
        RelativeLayout layoutGoodsNumber;
        @BindView(R.id.goods_unit)
        TextView goodsUnit;
        @BindView(R.id.layout_unit)
        RelativeLayout layoutUnit;
        @BindView(R.id.sku_number)
        TextView skuNumber;
        @BindView(R.id.layout_sku_number)
        RelativeLayout layoutSkuNumber;
        @BindView(R.id.info_out_warehouse_number)
        TextView infoOutWarehouseNumber;
        @BindView(R.id.layout_out_warehouse_number)
        RelativeLayout layoutOutWarehouseNumber;
        @BindView(R.id.info_out_warehouse_price)
        TextView infoOutWarehousePrice;
        @BindView(R.id.layout_out_warehouse_price)
        RelativeLayout layoutOutWarehousePrice;
        @BindView(R.id.info_money_total)
        TextView infoMoneyTotal;
        @BindView(R.id.layout_money_total)
        RelativeLayout layoutMoneyTotal;
        @BindView(R.id.info_remark)
        TextView infoRemark;
        @BindView(R.id.layout_remark)
        RelativeLayout layoutRemark;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
